package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.dto.*;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.order.OrderCoreService;
import com.mall.shopping.ICartService;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.shopping.dto.CartProductDto;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 郭宝 on 2020/11/15 16:55
 */
@RestController
@RequestMapping("/shopping")
@Anoymous
public class ShoppingController {

    @Reference(interfaceClass = IHomeService.class,check = false)
    IHomeService iHomeService;

    @Reference(interfaceClass = IContentService.class,check = false)
    IContentService iContentService;

    @Reference(interfaceClass = ICartService.class,check = false)
    ICartService iCartService;

    @Reference(interfaceClass = OrderCoreService.class,check = false)
    OrderCoreService orderCoreService;

    @Reference(interfaceClass = OrderQueryService.class,check = false)
    OrderQueryService orderQueryService;
    @RequestMapping("homepage")
    public ResponseData zzy1(){
        HomePageResponse homepage = iHomeService.homepage();
        ResponseData data = new ResponseData();
        data.setCode(200);
        data.setSuccess(true);
        data.setMessage("success");
        data.setResult(homepage.getPanelContentItemDtos());
        return data;
    }

    @RequestMapping("navigation")
    public ResponseData zzy2(){
        NavListResponse navListResponse = iContentService.queryNavList();
        ResponseData data = new ResponseData();
        data.setCode(200);
        data.setSuccess(true);
        data.setMessage("success");
        data.setResult(navListResponse.getPannelContentDtos());
        return data;
    }

    @Reference(interfaceClass = IProductService.class,check = false)
    IProductService productService;

    @GetMapping("categories")
    public ResponseData allProductCate(){

        List<ProductCateDto> productCateDto = productService.getCategories();

        return new ResponseUtil<>().setData(productCateDto);
    }

    @GetMapping("product/{id}")
    public ResponseData productDetail(@PathVariable("id") Long id){
        // 封装参数
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(id);

        ProductDetailResponse response = productService.getProductDetail(productDetailRequest);
        return new ResponseUtil<>().setData(response);
    }



    @RequestMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest){

        CreateOrderResponse order = orderCoreService.createOrder(createOrderRequest);

        return new ResponseUtil().setData(order.getOrderId(),"success");
    }

    //id为userId
    @RequestMapping("/items/{id}")
    public  ResponseData<String>  deleteCheckedItems(@PathVariable Long id){
        //校验userId
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(id);
        deleteCheckedItemRequest.requestCheck();
        //删除购物车中选中的商品
        iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        ResponseUtil<String> responseUtil = new ResponseUtil<>();
        return responseUtil.setData(ShoppingRetCode.SUCCESS.getMessage("000000"));
    }

    //获取当前用户的所有订单
    @GetMapping("order")
    public ResponseData getAllOrders(HttpServletRequest request, OrderListRequest orderListRequest ){
            Long userId = (Long) request.getAttribute("userInfo");
            orderListRequest.setUserId(userId);
            OrderListResponse orderListResponse  =  orderQueryService.queryAllOrders(orderListRequest);
            HashMap<String, Object> map = new HashMap<>();
            map.put("data",orderListResponse.getDetailInfoList());
            map.put("total",orderListResponse.getTotal());
            return new ResponseUtil<>().setData(map);
    }

    //查询订单详情
//    @RequestMapping("/order/{id}")
//    public ResponseData<String>
    @PutMapping("carts")
    public ResponseData freshCart(@RequestBody UpdateCartNumRequest updateCartNumRequest){
        UpdateCartNumResponse updateCartNumResponse = productService.freshCart(updateCartNumRequest);
        ResponseData<Object> objectResponseData = new ResponseUtil<>().setData(updateCartNumResponse);
        return objectResponseData;
    }



    @GetMapping("/order/{id}")
    public ResponseData orderQueryById(@PathVariable("id")String id){
        //封装请求参数
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(id);
        OrderResultVO orderResultVO =orderQueryService.orderQueryById(request);
        ResponseUtil<Object> objectResponseUtil = new ResponseUtil<>();
        ResponseData<Object> objectResponseData = objectResponseUtil.setData(orderResultVO);
        return objectResponseData;

    }
        /**
     * @param request
     * @return TODO:
     * 购物车列表
     */
    @GetMapping("carts")
    public ResponseData cartsList(HttpServletRequest request) {
        String userInfo = (String) request.getAttribute("userInfo");
        Long userID = Long.valueOf(userInfo);
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userID);
        CartListByIdResponse cartsList = iCartService.getCartListById(cartListByIdRequest);
        List<CartProductDto> cartProductDtos = cartsList.getCartProductDtos();
        ResponseData responseData = new ResponseUtil().setData(cartProductDtos, "success");

        return responseData;
    }

    /**
     *
     * @param map
     * @return
     * 购物车物品添加
     */
    @PostMapping("carts")
    public ResponseData addCarts(@RequestBody Map map) {
        Long userId = (Long) map.get("userId");
        Long productId = (Long) map.get("productId");
        Integer productNum = (Integer) map.get("productNum");
        AddCartRequest request = new AddCartRequest();
        request.setUserId(userId);
        request.setItemId(productId);
        request.setNum(productNum);
        AddCartResponse addCartResponse = iCartService.addToCart(request);
        return new ResponseUtil().setData("成功", "success");
    }

    /**
     * 取消订单
     */
    @PostMapping("order")
    public ResponseData cancelOrder(Long orderId) {
        orderCoreService.cancelOrder(orderId);
        return new ResponseUtil().setData("成功", "success");
    }

    /**
     * 删除订单
     */
    @DeleteMapping("order")
    public ResponseData deleteOrder(Long orderId) {
        int i = orderCoreService.deleteOrder(orderId);
        if (i == 1) {
            return new ResponseUtil().setData("成功", "success");
        }
        return new ResponseUtil().setErrorMsg("删除订单失败");
    }

}
